package db_connect;

import beans.Korisnik;
import beans.Sala;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author Milan Boricic <boricic@gmail.com>
 */
public class DB {
    public static Connection getConnection() throws SQLException {
        Connection con = null;
        String username = "milan";
        String password = "miki85";
        String driverClassName = "com.mysql.jdbc.Driver";
        String dburl = "jdbc:mysql://localhost:3306/lab_rezervacija";
        try {
            Class.forName(driverClassName);
            con = DriverManager.getConnection(dburl, username, password);
        } catch (ClassNotFoundException ex) {
            throw new SQLException(ex.getLocalizedMessage()+"ge");
        }
        return con;
    }

    public static Korisnik getKorisnik(Integer kid) {
        Korisnik korisnik = null;

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            String query = "select * from korisnik where kid=" + kid + ";";
            rs = stmt.executeQuery(query);
            if (rs.next()) {
                korisnik = new Korisnik();
                korisnik.setKid(kid);
                korisnik.setIme(rs.getString("ime"));
                korisnik.setPrezime(rs.getString("prezime"));
                korisnik.setEmail(rs.getString("email"));
                korisnik.setFunkcija(rs.getString("funkcija"));
                korisnik.setIsAdmin(rs.getBoolean("isAdmin"));
            }

            stmt.close();
            con.close();
        } catch(SQLException sqle) {
            sqle.printStackTrace();
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }
        return korisnik;
    }

    public static Sala getSala(Integer sid) {
        Sala sala = null;

        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;

        try {
            con = DB.getConnection();
            stmt = con.createStatement();
            String query = "select * from sala where sid=" + sid + ";";
            rs = stmt.executeQuery(query);
            if (rs.next()) {
                sala = new Sala();
                sala.setSid(sid);
                sala.setNaziv(rs.getString("naziv"));
                sala.setLokacija(rs.getString("Lokacija"));
                sala.setKapacitet(rs.getInt("Kapacitet"));
                sala.setBrRacunara(rs.getInt("BrRacunara"));
                sala.setImaTablu(rs.getBoolean("ImaTablu"));
                sala.setImaProjektor(rs.getBoolean("ImaProjektor"));
            }

            stmt.close();
            con.close();
        } catch(SQLException sqle) {
            sqle.printStackTrace();
            if (con != null) {
                try {
                    con.close();
                } catch (Exception exc) {
                }
            }
        }
        return sala;
    }
}
